<template>
	<div class="login_container">
		<div class="login_box">
			<div class="avatar_box">
				<img src="../assets/logo.png" alt="">
			</div>
			<el-form class="login-form" ref="loginForm" :model="form" :rules="loginFormRules">
				<el-form-item prop="username">
					<el-input prefix-icon="el-icon-user" placeholder="用户名" v-model="form.username"></el-input>
				</el-form-item>
				<el-form-item prop="password">
					<el-input prefix-icon="el-icon-lock" placeholder="密码" v-model="form.password" type="password"></el-input>
				</el-form-item>
				<el-form-item class="form-btn">
					<el-button type="primary" @click="formLogin">登录</el-button>
				</el-form-item>
			</el-form>
		</div>
	</div>
</template>

<script>
	export default {
		data(){
			return {
				form: {
					username: '',
					password: ''
				},
				loginFormRules: {
					username: [
						{ required: true, message: '请输入用户名', trigger: 'blur' },
						{ min: 3, max: 20, message: '长度在 3 到 20 个字符', trigger: 'blur' }
					],
					password:[
						{ required: true, message: '请输入密码', trigger: 'blur' },
						{ min: 6, max: 20, message: '长度在 6 到 20 个字符', trigger: 'blur' }
					]
				}
			}
		},
		methods: {
			formLogin(){
				this.$refs.loginForm.validate(async valid => {
					if(!valid) return ;
					const rest = await this.$http.post('/auth/login', this.form);
					window.sessionStorage.setItem("token", rest.data.token);
					await this.$router.push("/home");
				})
			}
		}
	}
</script>

<style lang="less" scoped>
.login_container{
	background-color: #2b4b6b;
	height: 100%;
	.login_box{
		background-color: #fff;
		width: 450px;
		height: 320px;
		position: absolute;
		border-radius: 3px;
		left: 50%;
		top: 50%;
		transform: translate(-50%, -50%);
		.avatar_box{
			width: 130px;
			height: 130px;
			border: 1px solid #eee;
			background-color: #fff;
			box-shadow: 0 0 10px #ddd;
			border-radius: 50%;
			padding: 10px;
			left: 50%;
			position: absolute;
			transform: translate(-50%, -50%);
			img {
				width: 100%;
				height: 100%;
				border-radius: 50%;
				background-color: #eee;
			}
		}
	}
}
.login-form{
	position: absolute;
	bottom: 0;
	width: 100%;
	padding: 0 20px;
	box-sizing: border-box;
	.form-btn{
		display: flex;
		justify-content: flex-end;
	}
}
</style>
